import { _decorator, Node, AudioClip, Component, Sprite, Color, v3, instantiate, Prefab, randomRangeInt } from 'cc';
import UI from "../../../../Script/Manager/UI";
import { bdlMgr } from "../../../../Script/Manager/BundleManager_S";
import { ColorBlock_GameData } from '../Data/ColorBlock_GameData';
import ColorBlock_GamePanel from '../UI/ColorBlock_GamePanel';
import ColorBlock_LosePanel from '../UI/ColorBlock_LosePanel';
import { ColorBlock_GameStorage } from '../Data/ColorBlock_GameStorage';
import ColorBlock_Block from '../Game/ColorBlock_Block';

const { ccclass, property, executionOrder } = _decorator;

// // 慢一点执行
@ccclass('ColorBlock_GameManager')
@executionOrder(101)
export default class ColorBlock_GameManager extends Component {
    static Instance: ColorBlock_GameManager;

    @property(Node)
    public Root_block: Node | null = null;
    @property(Node)
    public DeadLine: Node | null = null;
    @property(Node)
    public Button_parent: Node | null = null;

    Buttons = null;

    timer_speed: number = 0;
    timerCount_speed: number = 1;
    timerPlay_speed: boolean = true;

    speed = 200;

    onLoad() {
        ColorBlock_GameManager.Instance = this;

        this.Init();
    }

    Init() {
        ColorBlock_GameStorage.initData();
        ColorBlock_GameData.InitData();

        this.Buttons = this.Button_parent.getComponentsInChildren(Sprite);

        this.scheduleOnce(() => {
            ColorBlock_GameData.pause = false;

            ColorBlock_GamePanel.Instance.ShowOn();

            for (let index = 0; index < this.Buttons.length; index++) {
                const sp = this.Buttons[index];
                sp.color = new Color().fromHEX(ColorBlock_GameData.colors[index]);

                sp.node.on(Node.EventType.TOUCH_END, () => {
                    // console.log("点击了按钮", index);

                    let block = this.获取最下面的块();
                    // console.log("block---", block);

                    if (block) {
                        // block.node.scale = v3(0.8, 0.8, 0.8);
                        let id1 = index;
                        let id2 = block.id;
                        if (id1 === id2) {
                            block.Destroy();
                            ColorBlock_GameData.Add_score(10);
                            ColorBlock_GamePanel.Instance.Refresh();
                        }
                        else {
                            // console.log("颜色不匹配");
                            this.Lose();
                        }
                    }
                }, this);
            }
        }, 0.01);
    }

    update(dt: number): void {
        if (ColorBlock_GameData.pause) {
            return;
        }

        if (this.timerPlay_speed) {
            this.timer_speed += dt;
            if (this.timer_speed >= this.timerCount_speed) {
                this.timer_speed = 0;

                let id = randomRangeInt(0, ColorBlock_GameData.colors.length);
                let col = randomRangeInt(0, this.Buttons.length);
                ColorBlock_GameManager.CreateBlock(id, col, 0);
            }
        }

        this.Fall(dt * this.speed);


        for (let index = 0; index < ColorBlock_GameData.arrBlocks.length; index++) {
            const block: ColorBlock_Block = ColorBlock_GameData.arrBlocks[index];
            if (block.node.position.y <= this.DeadLine.position.y + 40) {
                this.Lose();
                break;
            }
        }
    }

    static async CreateBlock(_id: number, _col: number, _row: number,) {
        const prefab = await bdlMgr.getRes<Prefab>(`ColorBlock_Prefab`, `Block`, Prefab);
        const node: Node = instantiate(prefab);
        const block: ColorBlock_Block = node.getComponent(ColorBlock_Block);
        ColorBlock_GameManager.Instance.Root_block.addChild(node);
        block.init(_id, _col, _row);
        ColorBlock_GameData.arrBlocks.push(block);
        return block;
    }

    Fall(dt) {
        for (let index = 0; index < ColorBlock_GameData.arrBlocks.length; index++) {
            const block: ColorBlock_Block = ColorBlock_GameData.arrBlocks[index];
            let pos = block.node.position.clone();
            block.node.position = v3(pos.x, pos.y - dt, pos.z);
        }
    }

    获取最下面的块(): ColorBlock_Block {
        let nodes = ColorBlock_GameData.arrBlocks;
        if (ColorBlock_GameData.arrBlocks && ColorBlock_GameData.arrBlocks.length > 0) {
            let block = nodes[0];
            let minY = nodes[0].node.position.y;

            for (let i = 1; i < nodes.length; i++) {
                if (nodes[i].node.position.y < minY) {
                    minY = nodes[i].node.position.y;
                    block = nodes[i];
                }
            }

            return block;
        }

        return null;
    }

    是否失败() {
        return true;
    }

    Lose() {
        // await SSS_TimerManager.AwaitTime(1.4);
        ColorBlock_GameData.pause = true;
        UI.Instance.CreatePanel_sub(`ColorBlock_Prefab`, `UI/ColorBlock_LosePanel`, ColorBlock_LosePanel);
    }

    // Reborn() {
    // }
}

// /** 调试 */
window["ColorBlock_GameManager"] = ColorBlock_GameManager;